package com.qiyun.amqp.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Created by double.
 * @Date: 2018/8/21
 * @remarks:
 */

@Configuration
public class RabbitConfig {

    //交换机用于重新分配队列
    @Bean
    DirectExchange exchange() {
        return new DirectExchange("dttexchange",true,false);
    }
    @Bean
    DirectExchange repeatTradeChange() {
        return new DirectExchange("repeatTradeChange",true,false);
    }

    //用于延时消费的队列
    @Bean
    public Queue repeatTradeQueue() {
        Queue queue = new Queue("repeatTradeQueue",true,false,false);
        return queue;
    }



    //配置死信队列
   @Bean
    public Queue deadLetterQueue() {
        Map<String,Object> args = new HashMap<>();
        //args.put("x-message-ttl", 3000);
        args.put("x-dead-letter-exchange", "repeatTradeChange");
        args.put("x-dead-letter-routing-key", "repeatTradeQueue");
        return new Queue("deadLetterQueue", true, false, false, args);
    }

    //绑定交换机并指定routing key
    @Bean
    public Binding  repeatTradeBinding() {
        return BindingBuilder.bind(repeatTradeQueue()).to(repeatTradeChange()).with("repeatTradeQueue");
    }
    @Bean
    public Binding  dttBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(exchange()).with("deadLetterQueue");
    }
}